package com.quectel.util.common;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

@Slf4j
public class SocketUtil {

    public static boolean canConnect(String hostname, int port, int timeout) {
        Socket socket = new Socket();
        boolean isConnected = false;
        try {
            socket.connect(new InetSocketAddress(hostname, port), timeout);
            isConnected = socket.isConnected();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                log.warn("Be tested socket close failed", e);
            }
        }
        return isConnected;
    }

    public static void main(String[] args) {
        boolean canConnect = SocketUtil.canConnect("218.78.55.88", 1433, 5 * 1000);
        System.out.println(canConnect);
    }

}
